//
//  ImageStoreHelper.m
//  WoWeibo
//
//  Created by Jaedong n.Die_ on 12-5-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ImageStoreHelper.h"

@interface ImageStoreHelper (Private)

- (NSDate *)getLastDate;

@end

@implementation ImageStoreHelper

static ImageStoreHelper *storeHelper = nil;

+ (ImageStoreHelper *)sharedImageStoreHelper {

    if(storeHelper == nil) {
    
        storeHelper = [[super allocWithZone:NULL] init];
    }
    
    return storeHelper;
}

- (id)init {

    self = [super init];
    if(self) {
    
        _directoryPath = [[self.documentPath stringByAppendingPathComponent:@"Images"] retain];
        if([self checkFileOrDirectoryIsExists:_directoryPath] == NO) {
        
            [self createDirectory:_directoryPath];
        } 
    }
    
    return self;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [[self sharedImageStoreHelper] retain];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (NSUInteger)retainCount
{
    return NSUIntegerMax;
}

- (oneway void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;
}

- (void)dealloc {

    [_directoryPath release];
    
    _directoryPath = nil;
    
    [super dealloc];
}

#pragma mark - 
#pragma mark Instance Methods.

- (BOOL)storeImage:(UIImage *)image withName:(NSString *)name {

    return [self storeData:UIImageJPEGRepresentation(image, 1.0f) withName:name];
}

- (BOOL)storeData:(NSData *)data withName:(NSString *)name {

    return [self writeFile:[_directoryPath stringByAppendingPathComponent:name] withFileData:data];
}

- (UIImage *)getImageByName:(NSString *)name {

    NSData *data = [self getDataByName:name];
    if(data != nil) {
    
        return [UIImage imageWithData:data];
    }else {
    
        return nil;
    }
}

- (NSData *)getDataByName:(NSString *)name {

    return [self readFile:[_directoryPath stringByAppendingPathComponent:name]];
}

- (void)cleanImages {

    NSDate *now = [NSDate date];
    NSDate *lastModificationDate = [self getLastDate];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now];
    NSDateComponents *lastModificationComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:lastModificationDate];
    if(nowComponents.year != lastModificationComponents.year 
       || nowComponents.month != lastModificationComponents.month
       || nowComponents.day != lastModificationComponents.day) {
        
        [self deleteDirectory:_directoryPath];
        [self createDirectory:_directoryPath];
    }
    
    [calendar release];
}

#pragma mark -
#pragma mark Private Methods.

- (NSDate *)getLastDate {

    NSDictionary *properties = [[NSFileManager defaultManager] attributesOfItemAtPath:_directoryPath error:nil];
    NSDate *lastModificationDate = [properties objectForKey:NSFileModificationDate];
    
    return lastModificationDate;
}

@end
